import {
  getAddressList,
  deleteAddress,
  loginWithCode
} from "../../../api.js"
Page({
  data: {
    addressList: [],
    optionn:{},
    eventPrevent:true
  },
  onLoad(option) {
    var that = this
    console.log(option)
    if (option.stateTyp != 3){
      that.setData({
        eventPrevent:false,
        optionn: option
      })
    }
  },
  onShow: function (options) {
    getApp().startpage(new Date().getTime())
    let token = getApp().globalData.Store.token;
    getAddressList(token).then(res => {
      if (res.data.code === 666) {
        this.setData({
          addressList: res.data.data
        })
      } else if (res.data.code == 111111) {
        this.checkUserInfo()
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
  // 选中回退到页面, 订单信息通过Storage同步传递
  pickAddress(e) {
    console.log('222')
    var that = this
    if (that.data.optionn.stateTyp == 2){
      let id = e.currentTarget.dataset.id
      wx.setStorageSync("pickAddress", id)
      wx.navigateBack()
    }
    if (that.data.optionn.stateTyp == 1){
      var num = e.currentTarget.dataset.index
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        add_data: that.data.addressList[num]
      })
      wx.navigateBack()
    }
  },
  // 编辑按钮事件
  handleAddressEdit(e) {
    let id = e.target.dataset.id
    let editAddress = this.data.addressList.filter(a => a.id === id)
    wx.navigateTo({
      url: `/pages/myList/addList/address/address?address=${JSON.stringify(editAddress[0])}`,
    })
  },
  // 删除按钮事件
  handleAddressDelete(e) {
    let id = e.target.dataset.id
    let token = getApp().globalData.Store.token
    deleteAddress(token, [id]).then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code === 666) {
        this.setData({
          addressList: this.data.addressList.filter(a => a.id !== id)
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '网络错误，请稍候重试',
          icon: "none"
        })
        this.checkUserInfo()
      }
    })
  },

  // 检查用户信息授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          xp.setData({
            isAuth: false
          })
          wx.navigateTo({
            url: '/pages/shouquan/shouquan',
          })
        } else {
          xp.setData({
            isAuth: true
          })
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: getApp().globalData.businessId
        }).then(res => {
          if (res.data.code === 666) {
            getApp().globalData.Store = res.data.data
            xp.onShow()
          }
        })
      }
    })
  },
})